﻿using Core.Info;
using Core.Utility.Helper;
using Entity.Core.Enum.Dict;
using FreeSql.DataAnnotations;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;

namespace Entity.Core
{
    /// <summary>
    /// 公共 基础实体
    /// 包含
    /// <see cref="Id">Id</see>
    /// <see cref="Sid">雪花Id</see>
    /// <see cref="SortNumber">排序号</see>
    /// <see cref="Remark">备注</see>
    /// <see cref="CreatedBy">创建人外键</see>
    /// <see cref="CreatedByName">创建人姓名</see>
    /// <see cref="CreatedTime">创建时间</see>
    /// <see cref="UpdatedBy">更新人外键</see>
    /// <see cref="UpdatedByName">更新人姓名</see>
    /// <see cref="UpdatedTime">更新时间</see>
    /// <see cref="Revision">乐观锁</see>
    /// <see cref="IsDeleted">是否删除;(软删)</see>
    /// 等字段
    /// </summary>
    public class CommonBaseEntity : BaseEntity
    {
        /// <summary>
        /// Id，自增主键
        /// </summary>
        [JsonProperty, Column(Name = "id", IsIdentity = true, IsPrimary = true, Position = 1)]
        [Display(Name = "Id")]
        public virtual long? Id { get; set; }

        /// <summary>
        /// 雪花Id，所有的外键均使用该字段
        /// </summary>
        [JsonProperty, Column(Name = "sid",IsNullable = false, Position = 2)]
        [Display(Name = "雪花Id")]
        public virtual long Sid { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        [JsonProperty, Column(Name = "sort_number",IsNullable = false, Position = -11)]
        [Display(Name = "排序号")]
        public virtual long SortNumber { get; set; } = 0;

        /// <summary>
        /// 备注
        /// </summary>
        [JsonProperty, Column(Name = "remark", StringLength = 255, Position = -10)]
        [Display(Name = "备注")]
        public virtual string? Remark { get; set; }

        /// <summary>
        /// 创建人外键
        /// </summary>
        [JsonProperty, Column(Name = "created_by", IsNullable = false, Position = -9,CanUpdate = false)]
        [Display(Name = "创建人外键")]
        public virtual long? CreatedBy { get; set; }

        /// <summary>
        /// 创建人姓名
        /// </summary>
        [JsonProperty, Column(Name = "created_by_name", StringLength = 100, IsNullable = false, Position = -8, CanUpdate = false)]
        [Display(Name = "创建人姓名")]
        public virtual string? CreatedByName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [JsonProperty, Column(Name = "created_time", IsNullable = false, Position = -7, CanUpdate = false)]
        [Display(Name = "创建时间")]
        public virtual DateTime? CreatedTime { get; set; } = AppTime.Now;

        /// <summary>
        /// 更新人外键
        /// </summary>
        [JsonProperty, Column(Name = "updated_by", IsNullable = false, Position = -6)]
        [Display(Name = "更新人外键")]
        public virtual long? UpdatedBy { get; set; }

        /// <summary>
        /// 更新人姓名
        /// </summary>
        [JsonProperty, Column(Name = "updated_by_name", IsNullable = false, StringLength = 100, Position = -5)]
        [Display(Name = "更新人姓名")]
        public virtual string? UpdatedByName { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        [JsonProperty, Column(Name = "updated_time", IsNullable = false, Position = -4)]
        [Display(Name = "更新时间")]
        public virtual DateTime? UpdatedTime { get; set; } = AppTime.Now;

        /// <summary>
        /// 乐观锁
        /// </summary>
        [JsonProperty, Column(Name = "revision", IsVersion = true, Position = -3, IsNullable = false)]
        [Display(Name = "乐观锁")]
        public virtual long Revision { get; set; } = 0;

        #region 删除说明

        /// <summary>
        /// 删除说明
        /// </summary>
        [JsonProperty("deleted_description", Order = -2)]
        [Column(Name = "deleted_description", StringLength = 256, Position = -2)]
        [Display(Name = "删除说明")]
        public virtual string? DeletedDescription { get; set; }

        #endregion 删除说明

        /// <summary>
        /// 是否删除;用于标识数据是否逻辑删除(软删) 0:否;1:是
        /// </summary>
        [JsonProperty, Column(Name = "is_deleted", IsNullable = false, Position = -1)]
        [Display(Name = "是否删除")]
        public virtual int? IsDeleted { get; set; } = YesOrNoDictEnum.No.ToInt();
    }

    /// <summary>
    /// 公共 基础实体 接口
    /// 包含
    /// <see cref="Id">Id</see>
    /// <see cref="SortNumber">排序号</see>
    /// <see cref="Remark">备注</see>
    /// <see cref="CreatedBy">创建人外键</see>
    /// <see cref="CreatedByName">创建人姓名</see>
    /// <see cref="CreatedTime">创建时间</see>
    /// <see cref="UpdatedBy">更新人外键</see>
    /// <see cref="UpdatedByName">更新人姓名</see>
    /// <see cref="UpdatedTime">更新时间</see>
    /// <see cref="Revision">乐观锁</see>
    /// <see cref="IsDeleted">是否删除;(软删)</see>
    /// 等字段
    /// </summary>
    public interface ICommonBaseEntity
    {
        /// <summary>
        /// Id
        /// </summary>
        public int? Id { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int? SortNumber { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark { get; set; }

        /// <summary>
        /// 创建人外键
        /// </summary>
        public string? CreatedBy { get; set; }

        /// <summary>
        /// 创建人姓名
        /// </summary>
        public string? CreatedByName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreatedTime { get; set; }

        /// <summary>
        /// 更新人外键
        /// </summary>
        public string? UpdatedBy { get; set; }

        /// <summary>
        /// 更新人姓名
        /// </summary>
        public string? UpdatedByName { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdatedTime { get; set; }

        /// <summary>
        /// 乐观锁
        /// </summary>
        public long Revision { get; set; }


        /// <summary>
        /// 删除说明
        /// </summary>
        public string? DeletedDescription { get; set; }

        /// <summary>
        /// 是否删除;用于标识数据是否逻辑删除(软删) 0:否;1:是
        /// </summary>
        public int? IsDeleted { get; set; }
    }
}